Page({
  data: {
    // 订单状态筛选
    orderStatus: 'all', // all, unpaid, paid, completed
    // 模拟订单数据
    orders: [
      {
        id: 'OD20240501001',
        status: 'unpaid',
        statusText: '未付款',
        createTime: '2024-05-01 14:30',
        items: [
          { name: '商品A', price: 199, quantity: 1 }
        ],
        totalAmount: 199
      },
      {
        id: 'OD20240501002',
        status: 'paid',
        statusText: '已付款',
        createTime: '2024-05-01 10:15',
        items: [
          { name: '商品B', price: 299, quantity: 2 },
          { name: '商品C', price: 99, quantity: 1 }
        ],
        totalAmount: 697
      },
      {
        id: 'OD20240430001',
        status: 'completed',
        statusText: '已完毕',
        createTime: '2024-04-30 16:45',
        items: [
          { name: '商品D', price: 599, quantity: 1 }
        ],
        totalAmount: 599
      },
      {
        id: 'OD20240430002',
        status: 'unpaid',
        statusText: '未付款',
        createTime: '2024-04-30 09:30',
        items: [
          { name: '商品E', price: 159, quantity: 3 }
        ],
        totalAmount: 477
      },
      {
        id: 'OD20240429001',
        status: 'completed',
        statusText: '已完毕',
        createTime: '2024-04-29 14:20',
        items: [
          { name: '商品F', price: 299, quantity: 1 },
          { name: '商品G', price: 199, quantity: 1 }
        ],
        totalAmount: 498
      }
    ]
  },

  onLoad: function() {
    // 页面加载时执行的逻辑
    console.log('订单查看页面加载成功');
  },

  // 切换订单状态筛选
  onStatusChange: function(e) {
    const status = e.currentTarget.dataset.status;
    this.setData({
      orderStatus: status
    });
    console.log('切换订单状态筛选:', status);
  },

  // 点击订单项的处理函数
  onOrderTap: function(e) {
    const orderId = e.currentTarget.dataset.id;
    const order = this.data.orders.find(o => o.id === orderId);
    
    console.log('点击订单:', orderId);
    
    // 对于未付款订单，跳转到订单详情页
    if (order && order.status === 'unpaid') {
      // 转换为与订单详情页兼容的格式
      const goodsInfo = {
        id: order.id,
        title: order.items[0].name, // 使用第一个商品名称作为标题
        price: order.totalAmount,
        image: '/pages/tabBar/home.png', // 使用默认图片
        selectedService: '标准服务',
        details: '订单包含多个商品'
      };
      
      // 跳转到订单详情页
      wx.navigateTo({
        url: `/pages/order/order?goodsInfo=${encodeURIComponent(JSON.stringify(goodsInfo))}&fromOrderList=true`
      });
    }
  },
  
  // 取消订单功能
  cancelOrder: function(e) {
    const orderId = e.currentTarget.dataset.id;
    
    // 显示确认对话框
    wx.showModal({
      title: '取消订单',
      content: '确定要取消此订单并申请退款吗？',
      success: (res) => {
        if (res.confirm) {
          // 找到要取消的订单
          const orderIndex = this.data.orders.findIndex(o => o.id === orderId);
          
          if (orderIndex !== -1) {
            // 创建新的订单数组，不包含被取消的订单
            const newOrders = [...this.data.orders];
            newOrders.splice(orderIndex, 1);
            
            this.setData({
              orders: newOrders
            });
            
            wx.showToast({
              title: '订单已取消，退款将在1-3个工作日内退回原支付账户',
              icon: 'none',
              duration: 2000
            });
            
            console.log('订单已取消:', orderId);
          }
        }
      }
    });
  },

  // 刷新订单数据
  refreshOrders: function() {
    // 模拟从服务器获取数据
    setTimeout(() => {
      // 模拟新增一个订单
      const newOrder = {
        id: 'OD20240501003',
        status: 'paid',
        statusText: '已付款',
        createTime: new Date().toLocaleString('zh-CN'),
        items: [
          { name: '新品H', price: 399, quantity: 1 }
        ],
        totalAmount: 399
      };
      
      this.setData({
        orders: [newOrder, ...this.data.orders]
      });
      
      wx.showToast({
        title: '订单数据已刷新',
        icon: 'success'
      });
    }, 500);
  },

  // 下拉刷新处理函数
  onPullDownRefresh: function() {
    console.log('下拉刷新订单列表');
    // 调用刷新数据函数
    this.refreshOrders();
    // 停止下拉刷新动画
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 600);
  },
  
  // 根据状态获取筛选后的订单
  getFilteredOrders: function() {
    if (this.data.orderStatus === 'all') {
      return this.data.orders;
    }
    return this.data.orders.filter(order => order.status === this.data.orderStatus);
  }
})